import AppLayout from '@/layout/AppLayout.vue';
import { AuthGuard } from '@/utils/jwtUtils';
import { createRouter, createWebHistory } from 'vue-router';

// 路由配置
const routes = [
    {
        path: '/',
        component: AppLayout,
        meta: { requiresAuth: true },
        children: [
            {
                path: '',
                name: 'dashboard',
                component: () => import('@/views/Dashboard.vue'),
                meta: { title: '仪表板' }
            },
            {
                path: 'user/table/:departid',
                name: 'usertable',
                component: () => import('@/views/pages/UserTable.vue'),
                meta: { title: '用户列表' }
            },

            {
                path: 'questionTemplate',
                name: 'questionTemplate',
                component: () => import('@/views/pages/QuestionTemplate.vue'),
                meta: { title: '问题模板' }
            },
            {
                path: 'exam/management',
                name: 'exam-management',
                component: () => import('@/views/pages/ExamManagement.vue'),
                meta: { title: '试卷管理' }
            }
        ]
    },
    {
        path: '/client/:sn',
        name: 'ClientView',
        component: () => import('@/views/pages/ClientView.vue'),
        meta: { requiresAuth: false, title: '用户视图' },
        children: [
            {
                path: 'achievment',
                name: 'client-achievment',
                component: () => import('@/views/pages/Achievment.vue'),
                meta: { title: '用户成就' }
            },
            {
                path: 'chatlist',
                name: 'client-chatlist',
                component: () => import('@/views/pages/ChatList.vue'),
                meta: { title: '聊天列表' }
            },
            {
                path: 'profile',
                name: 'client-profile',
                component: () => import('@/views/pages/UserProfile.vue'),
                meta: { title: '用户资料' }
            }
        ]
    },
    {
        path: '/choujiang',
        name: 'choujiang',
        component: () => import('@/views/pages/ChouJiang.vue'),
        meta: { title: '抽奖系统' }
    },
    {
        path: '/userAnalysis/:sn',
        name: 'client-userAnalysis',
        component: () => import('@/views/pages/UserAnalysis.vue'),
        meta: { title: '用户分析' }
    },
    {
        path: '/chat/:sn',
        name: 'chat',
        component: () => import('@/views/pages/Chat.vue'),
        meta: { requiresAuth: false, title: '聊天' }
    },
    {
        path: '/exam/:sn',
        name: 'exam',
        component: () => import('@/views/pages/StudentExam.vue'),
        meta: { requiresAuth: false, title: '在线考试' }
    },
    {
        path: '/auth/login',
        name: 'login',
        component: () => import('@/views/pages/auth/Login.vue'),
        meta: { requiresAuth: false, title: '登录' }
    },
    {
        path: '/auth/access',
        name: 'access',
        component: () => import('@/views/pages/auth/Access.vue'),
        meta: { requiresAuth: false, title: '访问拒绝' }
    },
    {
        path: '/auth/error',
        name: 'error',
        component: () => import('@/views/pages/auth/Error.vue'),
        meta: { requiresAuth: false, title: '错误' }
    },
    {
        path: '/pages/notfound',
        name: 'notfound',
        component: () => import('@/views/pages/NotFound.vue'),
        meta: { requiresAuth: false, title: '页面未找到' }
    },
    {
        path: '/:pathMatch(.*)*',
        redirect: '/pages/notfound'
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
    // 设置页面标题
    if (to.meta.title) {
        document.title = `${to.meta.title} - EduWebManager`;
    }

    // 认证检查
    AuthGuard.checkAuth(to, from, next);
});

export default router;